package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessShareFarm;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.LogisticsListVO;
import com.digitalchina.lxnsy.entity.ShareOrderVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/9/28 18:46
 * 修改备注：
 * ================================================
 */
public class SelectLogisticsActivity extends BaseActivity {

    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.layout_name)
    RelativeLayout layoutName;
    @Bind(R.id.et_no)
    EditText etNo;
    @Bind(R.id.bt_submit)
    Button btSubmit;

    private List<LogisticsListVO> voList = new ArrayList<>();

    private String orderNo = "";
    private String deliveryCorp = ""; // 物流
    private String trackingNo = ""; // 单号
    private String deliveryCorpCode = ""; // 物流code


    private List<String> nameList = new ArrayList<>();
    private List<String> codeList = new ArrayList<>();

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_select_logistics);
        setTitle("确定发货");
    }

    @Override
    public void initMember() {

        ShareOrderVO vo = (ShareOrderVO) getIntent().getSerializableExtra("ShareOrderVO");
        orderNo = vo.getOrderNo();

//        BusinessShareFarm.getLogisticsCompanies(this, mHandler);
        InputStream inputStream = null;
        try {
            inputStream = getAssets().open("logistics.txt");
            int size = inputStream.available();
            int len = -1;
            byte[] bytes = new byte[size];
            inputStream.read(bytes);
            inputStream.close();
            String string = new String(bytes);
            voList = FastJsonUtil.getListBeanNoKey(string, LogisticsListVO.class);

        } catch (IOException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < voList.size(); i++) {
            nameList.add(voList.get(i).getName());
            codeList.add(voList.get(i).getCode());
        }

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_PICK_UP_VERIFICATION_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_PICK_UP_VERIFICATION_FIELD:
                        showToast(msg.obj.toString());
                        break;
                    case MSG.MSG_AFFIRM_SHIPMENTS_SUCCESS: // 确认发货
                        showToast("发货成功");
                        setResult(RESULT_OK);
                        finish();
                        break;
                    case MSG.MSG_AFFIRM_SHIPMENTS_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

//        voList = FastJsonUtil.getListBean(object.toString(), "", "", LogisticsListVO.class);

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.layout_name, R.id.bt_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_name:
                new MaterialDialog.Builder(SelectLogisticsActivity.this)
                        .title("请选择物流公司")
                        .items(nameList)
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                                deliveryCorp = nameList.get(position);
                                deliveryCorpCode = codeList.get(position);
                                tvName.setText(deliveryCorp);
                            }
                        })
                        .show();
                break;
            case R.id.bt_submit:
                trackingNo = etNo.getText().toString();
                if (StringUtil.isStrEmpty(tvName.getText().toString()) || StringUtil.isStrEmpty(trackingNo)) {
                    showToast("请完善信息");
                    return;
                }
                BusinessShareFarm.affirmShipments(SelectLogisticsActivity.this, orderNo, deliveryCorp, trackingNo, deliveryCorpCode, mHandler);
                break;
        }
    }
}
